package com.xiaoshuidi.cloud.module.finance.util;


import com.xiaoshuidi.cloud.module.finance.kdclient.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.axis.client.Stub;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.net.URL;

/**
 * @author dujianguang
 * */
@Component
@Slf4j
public class KingdeeUtil {
	private static String userName;
	@Value("${lease.kingdee.username}")
	public void setUserName(String userName) {
		KingdeeUtil.userName = userName;
	}

	private static String password;


	@Value("${lease.kingdee.password}")
	public void setPassword(String password) {
		KingdeeUtil.password = password;
	}


	private static String dcName;

	@Value("${lease.kingdee.dcName}")
	public void setDcName(String dcName) {
		KingdeeUtil.dcName = dcName;
	}

	private static String dbType;

	@Value("${lease.kingdee.dbType}")
	public void setDbType(String dbType) {
		KingdeeUtil.dbType = dbType;
	}

	private static String slnName;

	@Value("${lease.kingdee.slnName}")
	public void setSlnName(String slnName) {
		KingdeeUtil.slnName = slnName;
	}

	private static String easLoginUrl;

	@Value("${lease.kingdee.easLoginUrl}")
	public void setEasLoginUrl(String easLoginUrl) {
		KingdeeUtil.easLoginUrl = easLoginUrl;
	}

	private static String voucherUrl;

	@Value("${lease.kingdee.voucherUrl}")
	public void setVoucherUrl(String voucherUrl) {
		KingdeeUtil.voucherUrl = voucherUrl;
	}

	private static String header;
	@Value("${lease.kingdee.header}")
	public void setHeader(String header) {
		KingdeeUtil.header = header;
	}

	public static WSContext easLogin(){
		EASLoginProxy proxy = null;
		WSContext context = null;
		try {
			log.info("easLoginUrl{}",easLoginUrl);
			log.info("userName{}",userName);
			log.info("password{}",password);
			log.info("slnName{}",slnName);
			log.info("dcName{}",dcName);
			log.info("dbType{}",dbType);
			EASLoginProxyServiceLocator locator = new EASLoginProxyServiceLocator();
			//登录
			proxy = locator.getEASLogin();
			// 1.登录账号； 2.登录密码； 3.默认"eas"; 4.数据中心名称; 5.语言 （l1,l2,l3）一般默认"l2"; 6.数据库类型(MS SQL Server:0,DB2 UDB:1,Oracle:2,MS SQL Server:3)
			context = proxy.login(userName, password, slnName, dcName, "l2", Integer.valueOf(dbType));
		}catch (Exception e){
			e.printStackTrace();
		}
		return context;
	}

	public static String[] importVoucher(String sessionId, WSWSVoucher[] voucher){
		WSWSVoucherSrvProxyServiceLocator locator = new WSWSVoucherSrvProxyServiceLocator();
		try{
			WSWSVoucherSrvProxy wswsVoucher = locator.getWSWSVoucher();
			((Stub) wswsVoucher).setHeader(header, "SessionId",
					sessionId);
			String[][] is = wswsVoucher.importVoucherOfReturnID(voucher, 0, 0);
			System.out.println(is[0][0]);
			System.out.println(is[0][1]);
			System.out.println(is[0][2]);
			System.out.println(is[0][3]);
			System.out.println(is[0][4]);
			System.out.println(is[0][5]);
			System.out.println(is[0][6]);
			System.out.println(is[0][7]);
			return is[0];

		} catch (Exception ee) {
			ee.printStackTrace();
		}
		return null;
	}
}
